package io.github.xbeeant.eoffice.service.impl;

import io.github.xbeeant.core.ApiResponse;
import io.github.xbeeant.core.IdWorker;
import io.github.xbeeant.eoffice.antdesign.TreeNode;
import io.github.xbeeant.eoffice.config.AbstractSecurityMybatisPageHelperServiceImpl;
import io.github.xbeeant.eoffice.mapper.TagMapper;
import io.github.xbeeant.eoffice.model.Tag;
import io.github.xbeeant.eoffice.service.ITagService;
import io.github.xbeeant.spring.mybatis.pagehelper.IMybatisPageHelperDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * eoffice_tag
 */
@Service
public class TagServiceImpl extends AbstractSecurityMybatisPageHelperServiceImpl<Tag, Long> implements ITagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public IMybatisPageHelperDao<Tag, Long> getRepositoryDao() {
        return this.tagMapper;
    }

    @Override
    public void setDefaults(Tag record) {
        if (record.getTid() == null) {
            record.setTid(IdWorker.getId());
        }
    }

    @Override
    public List<TreeNode> tags(String userId) {
        Tag example = new Tag();
        example.setCreateBy(userId);
        ApiResponse<List<Tag>> listApiResponse = selectAllByExample(example);
        if (!listApiResponse.getSuccess()) {
            return Collections.emptyList();
        }
        List<TreeNode> list = new ArrayList<>(listApiResponse.getData().size());
        TreeNode treeNode;
        for (Tag tag : listApiResponse.getData()) {
            treeNode = new TreeNode();
            treeNode.setpKey(tag.getTid());
            treeNode.setTitle(tag.getTagname());
            list.add(treeNode);
        }
        return list;
    }
}
